<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="####1. 正则表达式re 模块使 Python 语言拥有全部的正则表达式功能。1). re.match函数re.match 尝试从字符串的起始位置匹配一个模式，如果不是起始位置匹配成功的话，match()就返回none。函数语法：1re.match(pattern, string, flags=0) 函数参数说明：pattern : 匹配的正则表达式string : 要匹配的字符串flags">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="Python-学习(六)">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/Python/Python-学习(六)/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="####1. 正则表达式re 模块使 Python 语言拥有全部的正则表达式功能。1). re.match函数re.match 尝试从字符串的起始位置匹配一个模式，如果不是起始位置匹配成功的话，match()就返回none。函数语法：1re.match(pattern, string, flags=0) 函数参数说明：pattern : 匹配的正则表达式string : 要匹配的字符串flags">
<meta property="og:locale" content="default">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-a076cf99cab9e840.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-da7b68c7d4e393c1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-5bf9fde93e4ead00.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-f954a47a49e3e334.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-8e70bd68d7f3e52e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-83e8e90096e0ff81.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-76c8144f2a09133e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-2a5b3743c98207b9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-ce7d2f0d4f83797f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/3110861-8ee04a60113dfa36.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2018-08-06T10:32:05.529Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Python-学习(六)">
<meta name="twitter:description" content="####1. 正则表达式re 模块使 Python 语言拥有全部的正则表达式功能。1). re.match函数re.match 尝试从字符串的起始位置匹配一个模式，如果不是起始位置匹配成功的话，match()就返回none。函数语法：1re.match(pattern, string, flags=0) 函数参数说明：pattern : 匹配的正则表达式string : 要匹配的字符串flags">
<meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/3110861-a076cf99cab9e840.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/Python/Python-学习(六)/"/>





  <title>Python-学习(六) | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/Python/Python-学习(六)/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Python-学习(六)</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:39+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>####1. 正则表达式<br>re 模块使 Python 语言拥有全部的正则表达式功能。<br>1). re.match函数<br>re.match 尝试从字符串的起始位置匹配一个模式，如果不是起始位置匹配成功的话，match()就返回none。<br>函数语法：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">re.match(pattern, string, flags=0)</span><br></pre></td></tr></table></figure></p>
<p>函数参数说明：<br>pattern : 匹配的正则表达式<br>string : 要匹配的字符串<br>flags : 标志位，用于控制正则表达式的匹配方式，如：是否区分大小写，多行匹配等等。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"># 正则表达式</span><br><span class="line">import re</span><br><span class="line"># 在起始位置开始匹配</span><br><span class="line">print(re.match(&quot;www&quot;,&quot;www.baidu.com&quot;).span())</span><br><span class="line"># 不在起始位置开始匹配</span><br><span class="line">print(re.match(&quot;com&quot;,&quot;www.baidu.com&quot;))</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-a076cf99cab9e840.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图1.png"><br><a id="more"></a></p>
<p>2). 使用group(num) 或 groups() 匹配对象函数来获取匹配表达式。</p>
<ul>
<li>group(num=0)：匹配的整个表达式的字符串，group() 可以一次输入多个组号，在这种情况下它将返回一个包含那些组所对应值的元组</li>
<li>groups()：返回一个包含所有小组字符串的元组，从 1 到 所含的小组号<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"># group</span><br><span class="line">import re</span><br><span class="line"># 字符串</span><br><span class="line">line = &quot;Cats are smarter than dogs&quot;</span><br><span class="line"># 正则匹配</span><br><span class="line">matchObj = re.match(r&apos;(.*) are (.*?) .*&apos;, line, re.M|re.I)</span><br><span class="line"># 判断是否匹配成功</span><br><span class="line">if matchObj:</span><br><span class="line">	print(&quot;matchObj.group(): &quot;, matchObj.group())</span><br><span class="line">	print(&quot;matchObj.group(1): &quot;, matchObj.group(1))</span><br><span class="line">	print(&quot;matchObj.group(2): &quot;, matchObj.group(2))</span><br><span class="line">	pass</span><br><span class="line">else:</span><br><span class="line">	print(&quot;No match!&quot;)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-da7b68c7d4e393c1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图2.png"></p>
<p>3). re.search方法<br>re.search 扫描整个字符串并返回第一个成功的匹配。<br>函数语法：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">re.search(pattern, string, flags=0)</span><br></pre></td></tr></table></figure></p>
<p>函数参数说明：<br>pattern : 匹配的正则表达式<br>string : 要匹配的字符串<br>flags : 标志位，用于控制正则表达式的匹配方式，如：是否区分大小写，多行匹配等等<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">import re</span><br><span class="line"># 在起始位置匹配</span><br><span class="line">print(re.search(&quot;www&quot;,&quot;www.baidu.com&quot;).span())</span><br><span class="line"># 不在起始位置匹配</span><br><span class="line">print(re.search(&quot;com&quot;,&quot;www.baidu.com&quot;).span())</span><br></pre></td></tr></table></figure></p>
<p>打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-5bf9fde93e4ead00.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图3.png"></p>
<p>我们可以使用group(num) 或 groups() 匹配对象函数来获取匹配表达式。</p>
<ul>
<li>group(num=0)：匹配的整个表达式的字符串，group() 可以一次输入多个组号，在这种情况下它将返回一个包含那些组所对应值的元组。</li>
<li>groups()：返回一个包含所有小组字符串的元组，从 1 到 所含的小组号。<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">import re</span><br><span class="line">line = &quot;Cats are smarter than dogs&quot;</span><br><span class="line">searchObj = re.search(r&quot;(.*) are (.*?) .*&quot;, line, re.M|re.I)</span><br><span class="line">if searchObj:</span><br><span class="line">	print(&quot;searchObj.group(): &quot;, searchObj.group())</span><br><span class="line">	print(&quot;searchObj.group(1): &quot;, searchObj.group(1))</span><br><span class="line">	print(&quot;searchObj.group(2): &quot;, searchObj.group(2))</span><br><span class="line">	pass</span><br><span class="line">else:</span><br><span class="line">	print(&quot;Nothing found!&quot;)</span><br><span class="line">	pass</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-f954a47a49e3e334.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图4.png"></p>
<p>4). <strong>re.match与re.search的区别</strong><br>re.match只匹配字符串的开始，如果字符串开始不符合正则表达式，则匹配失败，函数返回None；而re.search匹配整个字符串，直到找到一个匹配。</p>
<p>5). 检索和替换<br>Python 的re模块提供了re.sub用于替换字符串中的匹配项。<br>语法：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">re.sub(pattern, repl, string, count=0)</span><br></pre></td></tr></table></figure></p>
<p>参数：<br>pattern : 正则中的模式字符串。<br>repl : 替换的字符串，也可为一个函数。<br>string : 要被查找替换的原始字符串。<br>count : 模式匹配后替换的最大次数，默认 0 表示替换所有的匹配。</p>
<p><strong>repl为字符串</strong>示例：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">import re</span><br><span class="line">phone = &quot;2004-959-559 # 这个一个电话号码&quot;</span><br><span class="line"># 删除注释</span><br><span class="line">num = re.sub(r&apos;#.*$&apos;, &quot;&quot;, phone)</span><br><span class="line">print(&quot;电话号码：&quot;, num)</span><br><span class="line"></span><br><span class="line"># 移除非数字内容</span><br><span class="line">num = re.sub(r&apos;\D&apos;, &quot;&quot;, phone)</span><br><span class="line">print(&quot;电话号码：&quot;, num)</span><br></pre></td></tr></table></figure></p>
<p>打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-8e70bd68d7f3e52e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图5.png"></p>
<p><strong>repl为函数</strong>示例：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"># 将匹配的数字乘于2</span><br><span class="line">def double(matched):</span><br><span class="line">	value = int(matched.group(&apos;value&apos;))</span><br><span class="line">	return str(value*2)</span><br><span class="line">	pass</span><br><span class="line"></span><br><span class="line">s = &quot;A23G4HF56&quot;</span><br><span class="line">print(re.sub(&quot;(?P&lt;value&gt;\d+)&quot;, double, s))</span><br></pre></td></tr></table></figure></p>
<p>打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-83e8e90096e0ff81.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图6.png"></p>
<p>6). compile 函数<br>compile 函数用于编译正则表达式，生成一个正则表达式（ Pattern ）对象，供 match() 和 search() 这两个函数使用。<br>语法格式为：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">re.compile(pattern[, flags])</span><br></pre></td></tr></table></figure></p>
<p>参数：<br>pattern : 一个字符串形式的正则表达式<br>flags 可选，表示匹配模式，比如忽略大小写，多行模式等，具体参数为：<br>1&gt; re.I 忽略大小写<br>2&gt; re.L 表示特殊字符集 \w, \W, \b, \B, \s, \S 依赖于当前环境<br>3&gt; re.M 多行模式<br>4&gt; re.S 即为’ . ‘并且包括换行符在内的任意字符（’ . ‘不包括换行符）<br>5&gt; re.U 表示特殊字符集 \w, \W, \b, \B, \d, \D, \s, \S 依赖于 Unicode 字符属性数据库<br>6&gt; re.X 为了增加可读性，忽略空格和’ # ‘后面的注释<br>示例：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">import re</span><br><span class="line"># 用于匹配至少一个数字</span><br><span class="line">pattern = re.compile(r&apos;\d+&apos;)</span><br><span class="line"># 查找头部，没有匹配</span><br><span class="line">m = pattern.match(&apos;one12twothree34four&apos;)</span><br><span class="line"># 打印</span><br><span class="line">print(m)</span><br><span class="line"># 从‘e’的位置开始匹配，没有匹配</span><br><span class="line">m = pattern.match(&apos;one12twothree34four&apos;, 2, 10)</span><br><span class="line">print(m)</span><br><span class="line"># 从‘1’的位置开始匹配，正好匹配</span><br><span class="line">m = pattern.match(&apos;one12twothree34four&apos;, 3, 10)</span><br><span class="line">print(m)</span><br><span class="line"># group([group1, …]) 方法用于获得一个或多个分组匹配的字符串，当要获得整个匹配的子串时，可直接使用 group() 或 group(0)；</span><br><span class="line">print(m.group(0))</span><br><span class="line"># start([group]) 方法用于获取分组匹配的子串在整个字符串中的起始位置（子串第一个字符的索引），参数默认值为 0；</span><br><span class="line">print(m.start(0))</span><br><span class="line"># end([group]) 方法用于获取分组匹配的子串在整个字符串中的结束位置（子串最后一个字符的索引+1），参数默认值为 0；</span><br><span class="line">print(m.end(0))</span><br><span class="line"># span([group]) 方法返回 (start(group), end(group))。</span><br><span class="line">print(m.span(0))</span><br></pre></td></tr></table></figure></p>
<p>打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-76c8144f2a09133e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图7.png"></p>
<p>7). findall<br>在字符串中找到正则表达式所匹配的所有子串，并返回一个列表，如果没有找到匹配的，则返回空列表。<br><strong>注意： match 和 search 是匹配一次 findall 匹配所有。</strong><br>语法格式为：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">findall(string[, pos[, endpos]])</span><br></pre></td></tr></table></figure></p>
<p><strong>参数：</strong><br>string 待匹配的字符串。<br>pos 可选参数，指定字符串的起始位置，默认为 0。<br>endpos 可选参数，指定字符串的结束位置，默认为字符串的长度。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">import re</span><br><span class="line"># 查找数字</span><br><span class="line">pattern = re.compile(r&apos;\d+&apos;)</span><br><span class="line">result1 = pattern.findall(&apos;runoob 123 google 456&apos;)</span><br><span class="line">result2 = pattern.findall(&apos;runoob123google456&apos;, 0, 10)</span><br><span class="line">print(result1)</span><br><span class="line">print(result2)</span><br></pre></td></tr></table></figure></p>
<p>打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-2a5b3743c98207b9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图8.png"></p>
<p>8). finditer<br>和 findall 类似，在字符串中找到正则表达式所匹配的所有子串，并把它们作为一个迭代器返回。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">re.finditer(pattern, string, flags=0)</span><br></pre></td></tr></table></figure></p>
<p>参数：<br>pattern : 匹配的正则表达式<br>string : 要匹配的字符串<br>flags : 标志位，用于控制正则表达式的匹配方式，如：是否区分大小写，多行匹配等等<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">import re</span><br><span class="line">it = re.finditer(r&quot;\d+&quot;,&quot;12a32bc43jf3&quot;)</span><br><span class="line">for match in it:</span><br><span class="line">	print(match.group())</span><br><span class="line">	pass</span><br></pre></td></tr></table></figure></p>
<p>打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-ce7d2f0d4f83797f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图9.png"></p>
<p>####2. CGI(Common Gateway Interface)<br>CGI(Common Gateway Interface),通用网关接口,它是一段程序,运行在服务器上如：HTTP服务器，提供同客户端HTML页面的接口。<br>1). 网页浏览<br>为了更好的了解CGI是如何工作的，我们可以从在网页上点击一个链接或URL的流程：</p>
<ul>
<li>1、使用你的浏览器访问URL并连接到HTTP web 服务器。</li>
<li>2、Web服务器接收到请求信息后会解析URL，并查找访问的文件在服务器上是否存在，如果存在返回文件的内容，否则返回错误信息。</li>
<li>3、浏览器从服务器上接收信息，并显示接收的文件或者错误信息。<br>CGI程序可以是Python脚本，PERL脚本，SHELL脚本，C或者C++程序等。<br>2). CGI架构图<br><img src="http://upload-images.jianshu.io/upload_images/3110861-8ee04a60113dfa36.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图10-CGI架构图"><br>3). 程序<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">#!D:\java\python\python.exe  </span><br><span class="line">import cgi  </span><br><span class="line">form=cgi.FieldStorage()  </span><br><span class="line">name=form.getvalue(&apos;name&apos;,&apos;world&apos;)  </span><br><span class="line">print &quot;&quot;&quot;Content-type:text/html  </span><br><span class="line">  </span><br><span class="line">&lt;html&gt;  </span><br><span class="line">   &lt;head&gt;  </span><br><span class="line">     &lt;title&gt;Greeting Page&lt;/title&gt;  </span><br><span class="line">   &lt;/head&gt;  </span><br><span class="line">   &lt;body&gt;  </span><br><span class="line">     &lt;h1&gt;Hello.%s!&lt;/h1&gt;  </span><br><span class="line">       </span><br><span class="line">       </span><br><span class="line">     &lt;form action=&apos;simple3.cgi&apos;&gt;  </span><br><span class="line">     Change name &lt;input type=&apos;text&apos; name=&apos;name&apos;/&gt;  </span><br><span class="line">     &lt;input type=&apos;submit&apos; /&gt;  </span><br><span class="line">     &lt;/form&gt;  </span><br><span class="line">    &lt;/body&gt;  </span><br><span class="line">&lt;/html&gt;  </span><br><span class="line">&quot;&quot;&quot;%name</span><br></pre></td></tr></table></figure>
</li>
</ul>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/Python/Python-学习(八)--网络操作/" rel="next" title="Python-学习(八)--网络操作">
                <i class="fa fa-chevron-left"></i> Python-学习(八)--网络操作
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/Python/Python-学习(十)--数据解析/" rel="prev" title="Python-学习(十)--数据解析">
                Python-学习(十)--数据解析 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
